package com.kbbywyclient.ui.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.entity.ClerkDetailsData;
import com.kbbywyclient.di.AppComponent;
import com.kbbywyclient.di.module.ClerkDetailsModule;
import com.kbbywyclient.presenter.ClerkDetailsPresenter;
import com.kbbywyclient.ui.activity.base.BaseActivity;
import com.kbbywyclient.ui.adapter.ClerkDetailsAdapter;
import com.kbbywyclient.ui.contarct.ClerkDetailsContract;
import com.kbbywyclient.util.ImageLoaderUtils;
import com.kbbywyclient.util.StringUtil;
import com.kbbywyclient.util.ViewUtils;
import com.kbbywyclient.widget.CustomToolBar;
import com.kbbywyclient.widget.divider.VerticalItemDecoration;

import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Jiangzx on 2017/10/31 10:37.
 */

public class ClerkDetailsActivity extends BaseActivity implements ClerkDetailsContract.View{
    public static final String CLERK_LOGO_S="clerkLogo";
    public static final String CLERK_ID_I="clerkId";
    public static final String CLERK_NAME_S="clerkName";
    public static final String CLERK_MOBILE_S="clerkMobile";

    @BindView(R.id.v_toolbar)
    CustomToolBar toolBar;
    @BindView(R.id.app_bar)
    AppBarLayout appBar;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_clerk_name)
    TextView tvClerkName;

    @BindView(R.id.tv_left_top)
    TextView tvLeftTop;
    @BindView(R.id.tv_left_bottom)
    TextView tvLeftBottom;
    @BindView(R.id.tv_center_top)
    TextView tvCenterTop;
    @BindView(R.id.tv_center_bottom)
    TextView tvCenterBottom;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.tv_right_top)
    TextView tvRightTop;
    @BindView(R.id.tv_right_bottom)
    TextView tvRightBottom;

    @BindView(R.id.tv_item_left)
    TextView tvItemLeft;
    @BindView(R.id.tv_item_right)
    TextView tvItemRight;

    @BindView(R.id.recycle_view)
    RecyclerView recyclerView;

    @Inject
    ClerkDetailsPresenter presenter;

    private String logo;
    private int clerkId;
    private String clerkName;
    private String clerkTel;

    ClerkDetailsAdapter adapter;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_clerk_details;
    }

    @Override
    protected void initView() {
        toolBar.setLeftViewOnClickLinster(v->finish());
        int role=getUser().getRole();
        switch (role){
            case Constants.LEVEL_DISTRIBUTOR:
                tvLeftBottom.setText(R.string.text_total_application_with_unit);
                tvCenterBottom.setText(R.string.text_total_settlement_with_unit);
                llRight.setVisibility(View.GONE);

                tvItemLeft.setText(R.string.text_application_with_unit);
                tvItemRight.setText(R.string.text_settlement_with_unit);
                break;
            case Constants.LEVEL_FIRST:
            case Constants.LEVEL_SECOND:
                tvLeftBottom.setText(R.string.text_total_settlement_with_unit);
                tvCenterBottom.setText(R.string.text_total_reward_with_unit);
                tvRightBottom.setText(R.string.text_next_level_with_unit);

                tvItemLeft.setText(R.string.text_settlement_with_unit);
                tvItemRight.setText(R.string.text_reward_with_unit);
                break;
            default:
                tvLeftBottom.setText(R.string.text_total_settlement_with_unit);
                tvCenterBottom.setVisibility(View.GONE);
                llRight.setVisibility(View.GONE);

                tvItemLeft.setText(R.string.text_settlement_with_unit);
                tvItemRight.setVisibility(View.GONE);
                break;
        }

        final LinearLayoutManager layoutManager=
                new LinearLayoutManager(ClerkDetailsActivity.this,
                        LinearLayoutManager.VERTICAL,
                        false);
        VerticalItemDecoration decoration=
                new VerticalItemDecoration(Constants.DIVIDER_COLOR,1);
        int padding= StringUtil.dip2px(15);
        decoration.setPaddingLeftRight(padding,padding);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(newState==RecyclerView.SCROLL_STATE_IDLE&&
                        layoutManager.findFirstCompletelyVisibleItemPosition()==0){
                    appBar.setExpanded(true,true);
                }
            }
        });
        recyclerView.addItemDecoration(decoration);
    }

    @Override
    public void initData() {
        Intent intent=getIntent();
        logo=intent.getStringExtra(CLERK_LOGO_S);
        clerkId=intent.getIntExtra(CLERK_ID_I,0);
        clerkName=intent.getStringExtra(CLERK_NAME_S);
        clerkTel=intent.getStringExtra(CLERK_MOBILE_S);

        ImageLoaderUtils.displayImageDefaultRes(ivAvatar,
                API.PIC_ADDRESS+logo,
                R.mipmap.icon_head_portrait);
        ViewUtils.setText(tvClerkName,clerkName);

        presenter.loadClerkDetails(getUser().getSalesID(),clerkId);
    }

    @Override
    public void initAppComponent(AppComponent appComponent) {
        appComponent.getClerkDetailsComponent(new ClerkDetailsModule(this)).inject(this);
    }

    @OnClick(R.id.iv_call)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_call:
                if(!TextUtils.isEmpty(clerkTel)){
                    Intent intent=new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:"+clerkTel));
                    if(getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)!=null){
                        startActivity(intent);
                    }
                }
                break;
        }
    }

    @Override
    public void onClerkDetailsSuccess(ClerkDetailsData data) {
        int role=getUser().getRole();
        switch (role){
            case Constants.LEVEL_DISTRIBUTOR:
                //总代理
                tvLeftTop.setText(data.getAll_include());
                tvCenterTop.setText(data.getAll_settlement());
                break;
            case Constants.LEVEL_FIRST:
            case Constants.LEVEL_SECOND:
                //一级、二级合伙人
                tvLeftTop.setText(data.getAll_settlement());
                String reward=data.getAll_upper_prize_amount();
                if(TextUtils.isEmpty(reward)){
                    tvCenterTop.setText("0.00");
                }else{
                    tvCenterTop.setText(
                            String.format(Locale.CHINESE,
                                    Constants.AMOUNT_FORMAT,
                                    Double.parseDouble(reward)));
                }
                tvRightTop.setText(data.getCount());
                break;
            default:
                //普通合伙人
                tvLeftTop.setText(data.getAll_settlement());
                break;
        }
        List<ClerkDetailsData.MonthData> dataList=data.getEvery_month();
        adapter=new ClerkDetailsAdapter(dataList,getUser().getRole());
        recyclerView.setAdapter(adapter);
    }
}
